let table_name = "circle_list_table";
let table_data_url = "/sys/circle/list/list";
let number_list = "<'row'<'#mytool.col-xs-3'><'col-xs-9'>><'row'<'.col-xs-12'tr>><'row'<'#num_menu.col-xs-2'l><'col-xs-10'p>>";

function action_status() {
}

let dataManage = {
    currentItem: null,
    fuzzySearch: true,
    getQueryCondition: function (data) {
        let param = {};
        param.start = data.start;
        param.length = data.length;
        return JSON.stringify(param);
    },
};

let returnData = {
    getDate: [
        {
            className: "ellipsis",
            data: "",
            render: CONSTANT.DATA_TABLES.RENDER.ELLIPSIS,
            orderable: false,
            width: "50px"
        }, {
            className: "ellipsis",
            data: "name",
            render: CONSTANT.DATA_TABLES.RENDER.ELLIPSIS,
            orderable: false,
            width: "50px"
        },
        {
            className: "ellipsis",
            data: "img",
            orderable: false,
            width: "50px",
            render: function (data, type, row, meta) {
                if (isNotNull(row.img)) {
                    return '<img alt="" src="http://img.shengongying.cn/' + row.img + '" width="50px" height="50px">';
                }
            }
        }, {
            className: "ellipsis",
            data: "description",
            render: CONSTANT.DATA_TABLES.RENDER.ELLIPSIS,
            orderable: false,
            width: "50px"
        },{
            className: "ellipsis",
            data: "circleStatus",
            render: function (data, type, row, meta) {
                if (row.circleStatus === 0) {
                    return '上架';
                }else{
                    return "下架";
                }
            },
            orderable: false,
            width: "50px"
        },{
            className: "ellipsis",
            data: "weight",
            render:CONSTANT.DATA_TABLES.RENDER.ELLIPSIS,
            orderable: false,
            width: "50px"
        },
        {
            className: "ellipsis",
            data: "",
            orderable: false,
            width: "50px",
            render: function (data, type, row, meta) {
                let button = '';
                if (row.circleStatus === 0) {
                    button += '<button class="btn btn-sm btn-success" onclick=update_status(' + row.id + ',1)>下架</button>';
                } else {
                    button += '<button class="btn btn-sm btn-success" onclick=update_status(' + row.id + ',0)>上架</button>';
                }
                button += '<button class="btn btn-sm btn-danger" onclick=circle_delete(' + row.id + ')>删除</button>';
                button += '<button class="btn btn-sm btn-success" onclick=circle_detail(' + row.id + ')>详情</button>';
                return button;
            }
        }
    ]
}

function foot_info(result) {
}

function createdRow(row, data, index) {
    if (data.role) {
        $(row).addClass("info");
    }
}

function drawCallback(settings) {
}

function search_user() {
    find();
}

function fn_draw_call_back() {
}

function create_circle_list() {
    $('#circle_list_modal').modal('show');
}


function update_status(Id, type) {
    HttpUtils.post_json('/sys/circle/list/update/status', JSON.stringify({
        'id': Id,
        'status': type,
    }), function (result) {
        success_msg("更新成功", function () {
            find();
        });
    });
}

function circle_delete(Id) {
    HttpUtils.post_json('/sys/circle/list/delete', JSON.stringify({
        'id': Id,
    }), function (result) {
        success_msg("更新成功", function () {
            find();
        });
    });
}


function circle_detail(id) {
    $('#circle_id').val(id);
    HttpUtils.post_json("/sys/circle/list/detail", JSON.stringify({"id": id}), function (result) {
        let data = result.data;
        $('#name').val(data.name);
        $('#description').val(data.description);
        $('#recommend').val(data.recommend);
        $('#status').val(data.circleStatus);
        init_image_item("http://img.shengongying.cn/"+data.img);
        $('#weight').val(data.weight);
        $('#circle_list_modal').modal('show');
    });
}

modalClose('circle_list_modal', function () {
    $('#circle_id').val('');
    $('#name').val('');
    $('#description').val('');
    $('#recommend').val('');
    $('#status').val('');
    $('#weight').val('');
    image_item_re_init();
});

function save_circle_list() {
    let formData = new FormData();
    let id = $('#circle_id').val();
    formData.append("id", id);

    let name = $('#name').val();
    formData.append("name", name);

    let img = $('#img')[0].files[0];
    formData.append("picture", img);

    let description = $('#description').val();
    formData.append("description", description);

    let recommend = $('#recommend').val();
    formData.append("recommend", recommend);

    let status = $('#status').val();
    formData.append("status", status);

    let weight = $('#weight').val();
    formData.append("weight", weight);

    HttpUtils.upload_file('/sys/circle/list/save', formData, function (result) {
        success_msg("保存成功", function () {
            $('#circle_list_modal').modal('hide');
            find();
        });
    });
}
